package cn.b.sky.mes.mm.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.DistributionDao;
import cn.b.sky.mes.mm.dao.RequirementdistributionDao;
import cn.b.sky.mes.mm.enums.DistributionStatus;
import cn.b.sky.mes.mm.model.Distribution;
import cn.b.sky.mes.mm.model.RequirementDistribution;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.condition.DistributionCondition;
import cn.b.sky.mes.mm.model.condition.DistributionKanBanCondition;
import cn.b.sky.mes.mm.service.DistributionService;
import cn.b.sky.user.SecurityContextUtil;

@Service("distributionService")
public class DistributionServiceImpl implements DistributionService{

  @Autowired
  DistributionDao distributionDao;
  
  @Resource
  RequirementdistributionDao requirementdistributionDao;
  
  @Override
  public List<Distribution> getRequirementsKanBanByCondition(DistributionKanBanCondition condition) {
    if(condition==null) {
      return null;
    }
    if(StringUtils.isBlank(condition.getOrderField())){
      condition.setOrderField("createDateTime");
    }
    return distributionDao.selectRequirementsKanBanByCondition(condition);
  }

  @Override
  public int getRequirementsKanBanCountByCondition(DistributionKanBanCondition condition) {
    if(condition==null) {
      return 0;
    }
    return distributionDao.selectRequirementsKanBanCountByCondition(condition);
  }
  
  @Override
  public List<Distribution> selectDistributionListByCondition(DistributionCondition condition) {
    if (condition!=null) {
      return distributionDao.selectDistributionListByCondition(condition);
    }
    return null;
  }

  @Override
  public int selectDistributionCountByCondition(DistributionCondition condition) {
    if (condition!=null) {
      return distributionDao.selectDistributionCountByCondition(condition);
    }
    return 0;
  }

  @Override
  public Distribution getDistributionByNum(String siteCode, String distributionNum) {    
    if (!StringUtils.isBlank(siteCode) && !StringUtils.isBlank(distributionNum)) {
      return distributionDao.getDistributionByNum(siteCode, distributionNum);
    }
    return null;
  }

  @Override
  public void cancelDistribution(String siteCode, String distributionNum) {
    distributionDao.cancelDistribution(siteCode, distributionNum, DistributionStatus.CANCELED.getStatus(),SecurityContextUtil.getCurrentUserName(),new Date());
  }

  @Override
  public void confirmServedDistribution(String siteCode, String distributionNum, Double actualQuantity) {
    Distribution distribution = distributionDao.getDistributionByNum(siteCode, distributionNum);        
    distribution.setActualQuantity(distribution.getActualQuantity()+actualQuantity);
    //实发数量 等于 需配数量 关闭配送单
    if(distribution.getActualQuantity() - distribution.getDistributionQuantity()==0){
      distribution.setCloseUser(SecurityContextUtil.getCurrentUserName());
      distribution.setCloseDateTime(new Date());
      distribution.setStatusECode(DistributionStatus.CLOSED.getStatus());
    }
    distribution.setModifyUser(SecurityContextUtil.getCurrentUserName());
    distribution.setModifyDateTime(new Date());
    distributionDao.confirmServedDistribution(distribution); 
    List<RequirementDistribution> list = requirementdistributionDao.getRequirementDistributionBydistributionNum(siteCode,distributionNum);
    for (RequirementDistribution requirementDistribution : list) {
      if(actualQuantity>0){
        Double requirementQuantity = requirementDistribution.getRequirementQuantity();      
        Double distributionQuantity = requirementDistribution.getDistributionQuantity();
        Double drActualQuantity = requirementDistribution.getActualQuantity();
        Double quantity = requirementQuantity != null ? requirementQuantity-drActualQuantity : distributionQuantity-drActualQuantity; 
        requirementDistribution.setActualQuantity(requirementDistribution.getActualQuantity()+ (quantity > actualQuantity ? actualQuantity :  quantity));
        actualQuantity = actualQuantity-quantity;  
        requirementdistributionDao.updateActualQuantity(requirementDistribution);
      }      
    }
  }  

}
